Using Tensorflow 2.0 we call keras as a submodule of tensorflow. Below we create a sine wave and see how a dense layer network handles the prediction.
In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# Generate a sample sin wave
X = np.array([np.linspace(0,100,1000)])
y = np.sin(X)
# Plot the sample sin wave
plt.plot(y[0])
plt.show()
# Define a model to fit the above data
model = tf.keras.Sequential([
tf.keras.layers.Dropout(rate=0.2, input_shape=X.shape[1:]),
tf.keras.layers.Dense(units=64, activation='sigmoid'),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(units=1000, activation='sigmoid')
])
# Compile the model
model.compile(loss='mse',
optimizer='adam',
metrics=['accuracy'])
# Fit the model
model.fit(X,y, epochs=10)
# Get the prediction
y_hat = model.predict(X)
# Plot the results
plt.plot(y_hat[0])
plt.show()
Check the prediction after 1000 epochs
In [4]:
# Fit the model
model.fit(X,y, epochs=1000, verbose=0)
# Get the prediction
y_hat = model.predict(X)
# Plot the results
plt.plot(y_hat[0])
plt.show()
In [ ]: